package com.wxzz.elearing.user.api.admin;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.user.entity.BannerManage;
import com.wxzz.elearing.user.service.IBannerManageService;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/banner-manage")
@Api(tags = "banner图接口(后台)")
public class BannerManageController {

    @Autowired
    private IBannerManageService bannerManageService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "根据当前身份列出banner(后台)", notes = "根据当前身份列出banner(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listBannerManage", method = RequestMethod.POST)
    public ReturnVO<List<BannerManage>> listBannerManage(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return bannerManageService.listBannerManage(jsonObject.getLong("enterpriseId"));
    }

    @ApiOperation(value = " 新增banner图(后台)", notes = " 新增banner图(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveBannerManage", method = RequestMethod.POST)
    public ReturnVO saveBannerManage(@RequestHeader String token,@RequestBody BannerManage bannerManage) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        bannerManage.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        return bannerManageService.saveBannerManage(bannerManage);
    }

    @ApiOperation(value = " 修改banner图(后台)", notes = " 修改banner图(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "updateBannerManage", method = RequestMethod.POST)
    public ReturnVO updateBannerManage(@RequestBody BannerManage bannerManage) {
        return bannerManageService.updateBannerManage(bannerManage);
    }

    @ApiOperation(value = " 删除banner图(后台)", notes = " 删除banner图(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteBannerManage/{bannerManageId}", method = RequestMethod.POST)
    public ReturnVO deleteBannerManage(@PathVariable Long bannerManageId) {
        return bannerManageService.deleteBannerManage(bannerManageId);
    }
}
